package cn.hchaojie.lucene.query;

import java.io.IOException;

import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.BooleanQuery.Builder;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.BooleanClause.Occur;
import org.junit.Test;

public class DemoBooleanQuery {
	// 搜索标题必须含有”apache”的documents
	@Test
	public void testMust() throws IOException {
		IndexSearcher searcher = LuceneUtils.getIndexSearcher();

		Builder builder = new BooleanQuery.Builder();

		Query q = new TermQuery(new Term("name", "lucene"));
		
		builder.add(q, Occur.MUST);
		
		BooleanQuery booleanQuery = builder.build();
		
		TopDocs topDocs = searcher.search(booleanQuery, 100);
		
		LuceneUtils.printResult(searcher, topDocs);
	}
	
	// 搜索标题同时含有“apache”和“lucene”的documents
	@Test
	public void test2() throws IOException {
		IndexSearcher searcher = LuceneUtils.getIndexSearcher();
		
		// 创建bool查询
		Builder builder = new BooleanQuery.Builder();
		
		Query q1 = new TermQuery(new Term("name", "apache"));
		Query q2 = new TermQuery(new Term("name", "lucene"));
		
		builder.add(q1, Occur.MUST);	// 第一个条件必须满足
		builder.add(q2, Occur.MUST);	// 第二个条件必须满足
		
		BooleanQuery booleanQuery = builder.build();
		TopDocs topDocs = searcher.search(booleanQuery, 100);
		LuceneUtils.printResult(searcher, topDocs);
	}
	
	// 搜索标题同时含有“apache”但是不包含“lucene”的documents
	@Test
	public void test3() throws IOException {
		IndexSearcher searcher = LuceneUtils.getIndexSearcher();
		
		// 创建bool查询
		Builder builder = new BooleanQuery.Builder();
		
		Query q1 = new TermQuery(new Term("name", "apache"));
		Query q2 = new TermQuery(new Term("name", "lucene"));
		
		builder.add(q1, Occur.MUST);	// 第一个条件必须满足
		builder.add(q2, Occur.MUST_NOT);
		
		BooleanQuery booleanQuery = builder.build();
		TopDocs topDocs = searcher.search(booleanQuery, 100);
		LuceneUtils.printResult(searcher, topDocs);
	}
	
	// 搜索标题必须含有“apache”，同时最好含有“lucene”的document。
	@Test
	public void test4() throws IOException {
		IndexSearcher searcher = LuceneUtils.getIndexSearcher();
		
		// 创建bool查询
		Builder builder = new BooleanQuery.Builder();
		
		Query q1 = new TermQuery(new Term("name", "apache"));
		Query q2 = new TermQuery(new Term("name", "solr"));
		
		builder.add(q1, Occur.MUST);	// 第一个条件必须满足
		builder.add(q2, Occur.SHOULD);
		
		BooleanQuery booleanQuery = builder.build();
		
		System.out.println(booleanQuery.toString());

		TopDocs topDocs = searcher.search(booleanQuery, 100);
		LuceneUtils.printResult(searcher, topDocs);
	}
	
	// should + should 两个条件出现其中一个就可以
	@Test
	public void test5() throws IOException {
		IndexSearcher searcher = LuceneUtils.getIndexSearcher();
		
		// 创建bool查询
		Builder builder = new BooleanQuery.Builder();
		
		Query q1 = new TermQuery(new Term("name", "apache"));
		Query q2 = new TermQuery(new Term("name", "lucene"));
		
		builder.add(q1, Occur.SHOULD);
		builder.add(q2, Occur.SHOULD);
		
		BooleanQuery booleanQuery = builder.build();
		TopDocs topDocs = searcher.search(booleanQuery, 100);
		LuceneUtils.printResult(searcher, topDocs);
	}
}
